﻿using System.Collections.Generic;
using System.Linq;
using QFramework;
using UnityEngine;

namespace Gp.Scripts.Core
{
    public class LogChangeEvent
    {
        public LogDetail Detail { get; set; }

        public LogChangeEvent(LogDetail detail)
        {
            Detail = detail;
        }
    }
    
    
    public class LogDetail
    {
        public LogType Type;
        public string Content;

        public LogDetail(LogType type, string content)
        {
            Type = type;
            Content = content;
        }
    }
    
    public class LogModel : AbstractModel
    {
        private LinkedList<LogDetail> _logInner = new();
        public const int MaxLogNum = 200;
        
        

        protected override void OnInit()
        {
            
        }

        public void AddLog(LogType type, string content)
        {
            // 判断容量，如果超最大容量，弹出最早的一条
            if(_logInner.Count >= MaxLogNum){
                _logInner.RemoveFirst();
            }
            // 加入日志
            var log = new LogDetail(type, content);
            _logInner.AddLast(log);
            
            this.SendEvent(new LogChangeEvent(log));
        }

        public IEnumerable<string> GetLogList(List<LogType> typeList)
        {
            return _logInner.Where(i => typeList.Contains(i.Type)).Select(i => i.Content);
        }
    }
}